<template>
  <div class="app-container">

    <el-tabs style="margin-left: 16px;" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="商品限购" name="first"></el-tab-pane>
      <el-tab-pane label="用户限购" name="second"></el-tab-pane>
    </el-tabs>
    <div style="background-color: #fff" v-if="activeName == 'first'">
      <div class="container-module" style="padding: 10px 0 0 10px">
        <el-form :inline="true" class="search-form" size="small">
          <el-row style="display: flex">
            <div style="flex: 1">
              <el-form-item class="search-form-item" label="关键字" style="margin: 5px 0">
                <el-input clearable placeholder="请输入关键字" v-model="searchForm.params.keywords" class="input-with-select">
                </el-input>
              </el-form-item>
              <el-form-item class="search-form-item" label="供应商" style="margin: 5px 0">
                <el-select style="width: 100%" clearable v-model="searchForm.params.supplier_id" filterable
                  placeholder="请输入供货商名称搜索">
                  <el-option v-for="item in options" :key="item.id" :label="item.supplier_name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item class="search-form-item" label="商户" style="margin: 5px 0">
                <el-select style="width: 100%" clearable v-model="searchForm.params.mer_id" filterable
                  placeholder="请输入商户名称搜索">
                  <el-option v-for="item in merchantList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div style="padding: 5px 0">
              <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-search" @click="getcatelist">查询</el-button>
                <el-button size="small" type="primary" icon="el-icon-refresh" @click="resetting">重置</el-button>
                <el-button v-if="!searchDisplay" type="warning" icon="el-icon-caret-bottom"
                  @click="searchDisplay = true">展开</el-button>
                <el-button v-else type="warning" icon="el-icon-caret-top" @click="searchDisplay = false">收起</el-button>
              </el-form-item>
            </div>
          </el-row>
          <el-row v-if="searchDisplay" style="display: flex">
            <el-form-item class="search-form-item" label="分类" style="margin: 5px 0">
              <el-cascader clearable v-model="searchForm.params.cate_ids" :options="typeData" :props="{
                  expandTrigger: 'hover',
                  checkStrictly: true,
                  value: 'id',
                  label: 'category_name',
                }" @change="handleChange" placeholder="请选择类目ID"></el-cascader>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
      <el-divider></el-divider>
      <div style="padding: 0 20px 20px">
        <div class="marginBottom">
          <el-row :gutter="20">
            <el-col :span="21">
              <div class="left-panel">
                <el-button icon="el-icon-circle-plus-outline" type="primary" size="small"
                  @click="goodsadd()">选配商品</el-button>
              </div>
            </el-col>

            <el-col :span="3" style="text-align: right;">
              <el-button type="warning" size="small" @click="submitForms()">保存当前配置</el-button>
            </el-col>
          </el-row>
        </div>
        <el-row :gutter="20">
          <el-form ref="allocationform" :model="formData" :rules="formRules" inline>
            <el-col>
              <el-table ref="multipleTable" v-loading="loading" :data="formData.tableData" :header-cell-style="{
                'background-color': '#e5eeff',
                color: '#515a6e',
              }" @selection-change="handleSelectionChange" style="width: 100%;">
                <el-table-column type="selection" width="60">
                </el-table-column>
                <el-table-column prop="name" label="商品名称" align="center">
                  <template slot-scope="scope">
                    <el-tooltip placement="top-start" :content="scope.row.name + ''">
                      <div class="one_line_hidden">
                        {{ scope.row.name ? scope.row.name : "-" }}
                      </div>
                    </el-tooltip>
                  </template>
                </el-table-column>
                <el-table-column prop="category_id1" label="商品分类" align="center" width="120">
                </el-table-column>
                <el-table-column prop="main_image" label="商品图片" align="center" width="120">
                  <template slot-scope="scope">
                    <div style="width: 36px; height: 36px; margin: 0 auto">
                      <el-image v-if="scope.row.main_image" :src="scope.row.main_image"
                        :preview-src-list="[scope.row.main_image]" />
                    </div>
                  </template>
                </el-table-column>
                <el-table-column prop="supplier" label="所属店铺" align="center" width="120">
                  <template slot-scope="scope">
                    <el-tooltip placement="top-start" :content="scope.row.supplier.supplier_name + ''">
                      <div class="one_line_hidden">
                        {{ scope.row.supplier ? scope.row.supplier.supplier_name : "-" }}
                      </div>
                    </el-tooltip>
                  </template>
                </el-table-column>
                <el-table-column prop="max_price" label="商品价格" align="center" width="120">
                </el-table-column>

                <el-table-column label="日限购数量" align="center" width="160">
                  <template slot-scope="scope">
                    <el-col style="margin-top:10px">
                      <el-form-item :prop="`tableData.${scope.$index}.daily_purchase`" :rules="formRules.newFieldName">
                        <el-input controls-position="right" v-model="scope.row.daily_purchase" placeholder="数量">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </template>
                </el-table-column>
                <el-table-column label="周限购数量" align="center" width="160">
                  <template slot-scope="scope">
                    <el-col style="margin-top:10px">
                      <el-form-item :prop="`tableData.${scope.$index}.week_purchase`" :rules="formRules.newFieldName">
                        <el-input controls-position="right" v-model="scope.row.week_purchase" placeholder="数量">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </template>
                </el-table-column>
                <el-table-column label="月限购数量" align="center" width="160">
                  <template slot-scope="scope">
                    <el-col style="margin-top:10px">
                      <el-form-item :prop="`tableData.${scope.$index}.month_purchase`" :rules="formRules.newFieldName">
                        <el-input controls-position="right" v-model="scope.row.month_purchase" placeholder="数量">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </template>
                </el-table-column>
                <el-table-column label="总限购数量" align="center" width="160">
                  <template slot-scope="scope">
                    <el-col style="margin-top:10px">
                      <el-form-item :prop="`tableData.${scope.$index}.total_purchase`" :rules="formRules.newFieldName">
                        <el-input controls-position="right" v-model="scope.row.total_purchase" placeholder="数量">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </template>
                </el-table-column>
                <el-table-column fixed="right" prop="address" label="操作" align="center" width="130px">
                  <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="goodsremove(scope.row.id)">
                      移除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table></el-col>
          </el-form>
          <el-col>
            <pagination v-show="total > 0" :total="total" :page.sync="searchForm.page" :limit.sync="searchForm.count"
              @pagination="getcatelist" />
          </el-col>
        </el-row>
      </div>
    </div>
    <!--  -->
    <div v-else>
      <el-tabs style="margin-left: 16px;" v-model="activeNames" @tab-click="handleClicks">
        <el-tab-pane label="所有用户" name="firsts"></el-tab-pane>
        <el-tab-pane label="特权用户" name="seconds"></el-tab-pane>
      </el-tabs>
      <!-- 所有用户 -->
      <div v-if="activeNames == 'firsts'">
        <el-alert title="设置参数保存后，对平台所有用户生效" type="warning">
        </el-alert>
        <div class="container-module" style="margin-top: 10px;">
          <el-form v-loading="loading" ref="eventsForm" label-position="left" label-width="100px" :model="eventsForm"
            size="small" :rules="formRuless">
            <div class="form-box">
              <el-row>
                <el-col :span="6">
                  <el-form-item label="日限购数量" prop="daily_purchase" :rules="formRuless.newFieldName">
                    <el-input style="width:200px;" placeholder="请输入数量" controls-position="right"
                      v-model.trim="eventsForm.daily_purchase">
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="周限购数量" prop="week_purchase" :rules="formRuless.newFieldName">
                    <el-input style="width:200px;" placeholder="请输入数量" controls-position="right"
                      v-model.trim="eventsForm.week_purchase">
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="月限购数量" prop="month_purchase" :rules="formRuless.newFieldName">
                    <el-input style="width:200px;" placeholder="请输入数量" controls-position="right"
                      v-model.trim="eventsForm.month_purchase">
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="总限购数量" prop="total_purchase" :rules="formRuless.newFieldName">
                    <el-input style="width:200px;" placeholder="请输入数量" controls-position="right"
                      v-model.trim="eventsForm.total_purchase">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </div>
        <div style="margin: 50px 0 0 50px;">
          <el-button style="width: 80px" type="primary" :loading="butloading" @click="gradeSubmitForm()">保存</el-button>
          <el-button style="width: 80px;margin-left: 39px;" type="info" @click="resetForm()">重置</el-button>
        </div>
      </div>
      <!-- 特权用户 -->
      <div v-else>
        <userlist ref="refuserlist" />
      </div>
    </div>
    <batch ref="refbatch" />
    <productConfig v-if="ifShowProductConfig"></productConfig>
  </div>
</template>

<script>
  import batch from "./components/batch";
  import userlist from "./components/userlist";
  import addproduct from "./components/addproduct";
  import productConfig from "./components/productConfig";
  import {
    pluginsgetList,
  } from "@/api/index";
  import {
    getMerchantList,
  } from '@/api/merchant/index'
  import {
    supplierLists,
    getCateLis
  } from "@/api/supplier/index";
  export default {
    components: {
      batch,
      productConfig,
      userlist
    },
    data() {
      return {
        butloading: false,
        eventsForm: {
          daily_purchase: '',
          week_purchase: '',
          month_purchase: '',
          total_purchase: '',
        },
        activeNames: 'firsts',
        activeName: 'first',
        ifShowProductConfig: false,
        //表单校验
        formRules: {
          newFieldName: [{
            required: false,
            validator: (rule, value, callback) => {
              let req = /^\d+$/;
              if (req.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正整数"));
              }
            },
            trigger: "change",
          }],
        },
        searchDisplay: false,
        searchForm: {
          page: 1,
          count: 10,
          params: {
            supplier_id: '',
            mer_id: '',
            keywords: '',
            cate_ids: ''
          }
        },
        formData: {
          tableData: []
        },
        loading: false, //刷新
        total: 0,
        multipleSelection: [],
        options: [],
        typeData: [],
        merchantList: []
      };
    },
    watch: {},
    mounted() {
      this.SupplierLists()
      this.getcartlist()
      this.getListWay()
      // this.getcatelist()
    },
    methods: {
      //重置
      resetForm(){
        this.eventsForm = {
         daily_purchase:'',
         week_purchase:'',
         month_purchase:'',
         total_purchase:'',
        }
        this.$refs.eventsForm.clearValidate();
      },
      //保存
      gradeSubmitForm() {
        this.$refs.eventsForm.validate((valid, error) => {
          if (valid) {
            this.butloading = true
            this.$nextTick(() => {
              this.butloading = false
              console.log(this.eventsForm);
            });
          } else {}
        });
      },
      //切换tab
      handleClick(tab, event) {

      },
      //切换tab
      handleClicks(tab, event) {

      },
      //商户
      getListWay() {
        let param = {
          page: 1,
          count: 100
        }
        getMerchantList(param)
          .then((res) => {
            this.merchantList = res.data.list
          })
          .catch(() => {})
      },
      //切换搜索分类
      handleChange(value) {
        const length = value.length;
        const lastValue = value[length - 1];
        this.searchForm.cate_ids = lastValue;

      },
      //类目列表
      getcartlist() {
        getCateLis().then((res) => {
          this.typeData = res.data;
        });
      },
      //供应商列表
      SupplierLists() {
        supplierLists().then((res) => {
          this.options = res.data;
        });
      },
      closeConfig() {
        this.ifShowProductConfig = false;
      },
      goodsremove(idToRemove) {

      },
      //添加商品弹窗打开
      goodsadd() {
        this.ifShowProductConfig = true
      },
      // 提交
      submitForms() {
        this.$refs.allocationform.validate((valid) => {
          if (valid) {

          } else {
            this.$messages.error('您有部分配置未完成');
            return false;
          }
        })
      },
      //勾选
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      //选择
      handleSelectionChange(val) {
        this.multipleSelection = val;

      },
      getcatelist() {
        this.loading = true;
        pluginsgetList(this.searchForm)
          .then((res) => {
            this.loading = false;
            if (res.code !== 200 || res.status !== 1) {
              this.$messages.error(res.msg);
              return;
            }
            this.formData.tableData = res.data.list;
            this.total = res.data.total;
          })
          .catch(() => {
            this.loading = false;
          });
      },
      resetting() {
        this.searchForm = {
          supplier: '',
          page: 1,
          count: 10,
          params: {
            supplier_id: '',
            mer_id: '',
            keywords: '',
            cate_ids: ''
          }
        };
      },
      //页码
      handleSizeChange(val) {},
      handleCurrentChange(val) {},
      //点击搜索
      search() {
        this.$messages({
          type: "success",
          message: "查询成功!",
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .title {
    color: rgba(0, 0, 0, 0.85);
    font-weight: 500;
    font-size: 16px;
    line-height: 16px;
  }

  .sn {
    color: #606266;
    font-size: 13px;
  }

  .demo-table-expand {
    display: flex;
    justify-content: start;
    flex-wrap: wrap;
    color: #515a6e;

    div {
      width: 30%;
      height: 40px;
      text-align: center;
      line-height: 40px;
      font-size: 12px;
    }
  }

  .marginTop {
    padding: 0 15px !important;
  }
</style>
